<?php

namespace App\Http\Controllers\Mini\Shop;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\GoodsClassService;
use App\Services\StoreService;
use App\Services\GoodsService;

class StoreController extends Controller
{

    /**
     * 2021/3/18--10:06
     * @param StoreService $store_service
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:index
     * @effect:店铺列表
     */
    public function index(StoreService $store_service)
    {
        $info = $store_service->getHomeStoreList();
        if ($info['status']) {
            return $this->success($info['data']);
        } else {
            return $this->error($info['msg']);
        }
    }


    /**
     * 获取店铺信息
     * @param StoreService $store_service
     * @param GoodsService $goods_service
     * @param $id
     * @return array
     */
    public function get_store_info(StoreService $store_service, GoodsService $goods_service, GoodsClassService $goodsClassService)
    {

        $store_info = $store_service->getStoreInfoAndRate(request()->id, 'id,store_name,store_company_name,area_info,store_address,store_slide,store_logo,customer_server_phone');

        if ($store_info['status']) {
            $slide = [];
            if (!empty($store_info['data']['store_slide'])) {
                $slide_demo = explode(',', $store_info['data']['store_slide']);
                foreach ($slide_demo as $v) {
                    $slide[] = ['image_url' => $v];
                }
            }
            $order_model = new Order();
            $store_info['data']['store_slide'] = $slide;
            $store_info['data']['comment_rate'] = $store_info['data']['comments_count'] == 0 ? 100 : round((($item->good_comment ?? 0) / $item->comments_count) * 100, 2);
            $store_info['data']['sale'] = $order_model->where('store_id', $store_info['data']['id'])->where('order_status', 6)->count();
            $store_info['data']['cate'] = $goodsClassService->getGoodsClassInfo($store_info['data']['id'])['data'];
            return $this->format($store_info['data']);
        }
    }

    // 获取店铺首页的商品
    public function get_store_goods_list(GoodsService $goods_service)
    {
        $info = $goods_service->getHomeStoreGoods(request()->id);
        if ($info['status']) {
            return $this->success($info['data']);
        } else {
            return $this->error($info['msg']);
        }
    }


    // 根据用户ID获取店铺信息
    public function store_verify(StoreService $store_service)
    {
        $info = $store_service->getStoreVerify();
        if ($info['status']) {
            return $this->success($info['data']);
        } else {
            return $this->error($info['msg']);
        }
    }

}
